package com.jse.io.nio.mynio;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class AClient {
	public static void main(String[] args) throws Exception {
		SocketChannel client = SocketChannel.open();
		client.configureBlocking(false);
		Selector selector = Selector.open();
		client.register(selector, SelectionKey.OP_CONNECT);
		client.connect(new InetSocketAddress(InetAddress.getLocalHost(), 8080));

		while (true) {
			selector.select();
			// readKeys
			Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
			while (iter.hasNext()) {
				SelectionKey key = iter.next();
				iter.remove();

				if (key.isConnectable()) {
					// System.out.println("connnect");
					SocketChannel socket = (SocketChannel) key.channel();
//					socket.configureBlocking(false);
//					 socket.register(selector, SelectionKey.OP_READ);
					// System.out.println("client is connected");
				} else if (key.isReadable()) {
					System.out.println("client is read");
				} else if (key.isWritable()) {
//					client.write(src);
				}
			}
			
			Thread.sleep(3000);
		}
	}
}
